package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCdkey;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallCdkeyService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/cdkey")
@Validated
public class AdminCdkeyController {
    private final Log logger = LogFactory.getLog(AdminCdkeyController.class);

    @Autowired
    private LitemallCdkeyService cdkeyService;

    @RequiresPermissions("admin:cdkey:list")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String companyName, String status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCdkey> cdkeyList = cdkeyService.querySelective(companyName, status,  page, limit, sort, order);
        return ResponseUtil.okList(cdkeyList);
    }

    private Object validate(LitemallCdkey cdkey) {

        return null;
    }

    @RequiresPermissions("admin:cdkey:create")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallCdkey cdkey) {
        Object error = validate(cdkey);
        if (error != null) {
            return error;
        }cdkeyService.add(cdkey);
        return ResponseUtil.ok(cdkey);
    }

    @RequiresPermissions("admin:cdkey:read")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallCdkey cdkey = cdkeyService.findById(id);
        return ResponseUtil.ok(cdkey);
    }

    @RequiresPermissions("admin:cdkey:update")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallCdkey cdkey) {
        Object error = validate(cdkey);
        if (error != null) {
            return error;
        }
        if (cdkeyService.updateById(cdkey) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(cdkey);
    }

    @RequiresPermissions("admin:cdkey:delete")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallCdkey cdkey) {
        Integer id = cdkey.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        cdkeyService.deleteById(id);
        return ResponseUtil.ok();
    }



    @RequiresPermissions("admin:cdkey:read")
    @RequiresPermissionsDesc(menu = {"企业管理", "兑换码管理"}, button = "下载")
    @GetMapping("/export")
    public Object export(Integer companyId, Integer chargeId) {
        List<LitemallCdkey> cdkeyList = cdkeyService.queryByCompanyIdAndChargeId(companyId, chargeId);

        return ResponseUtil.okList(cdkeyList);
    }


}
